package justbuy.payment.service;

import javax.inject.Inject;

import org.ironrhino.common.support.SettingControl;
import org.springframework.beans.factory.annotation.Autowired;

public abstract class AbstractPaymentService implements PaymentService {

	@Inject
	protected SettingControl settingControl;

	@Autowired(required = false)
	protected TradeHandler tradeHandler;

	public String getName() {
		return getClass().getSimpleName().toLowerCase();
	}

	public boolean isEnabled() {
		return settingControl.getBooleanValue("payment." + getName()
				+ ".enabled", true);
	}

	public int getDisplayOrder() {
		return settingControl.getIntValue("payment." + getName()
				+ ".displayOrder", 0);
	}

	public String getOrderUrl(String orderCode) {
		return tradeHandler.getOrderUrl(orderCode);
	}

	public String toString() {
		return getName();
	}

	public int compareTo(PaymentService object) {
		if (!(object instanceof AbstractPaymentService))
			return 0;
		AbstractPaymentService ap = (AbstractPaymentService) object;
		if (this.getDisplayOrder() != ap.getDisplayOrder())
			return this.getDisplayOrder() - ap.getDisplayOrder();
		return this.toString().compareTo(ap.toString());
	}
}
